package com.tem.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tem.common.core.base.BaseController;
import com.tem.common.core.domain.SysSearchPage;
import com.tem.common.log.annotation.Log;
import com.tem.common.log.enums.BusinessType;
import com.tem.common.tool.result.ResultEnum;
import com.tem.common.tool.result.ResultUtil;
import com.tem.system.entity.SysUserEntity;
import com.tem.system.service.SysUserService;
import com.tem.system.vo.SysUserPwdVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 腾木科技 Created by 2022/1/12 15:59
 */
@Api(tags = "操作员管理API")
@RestController
@RequestMapping(value = "/operator")
public class SysUserController extends BaseController {

    private final SysUserService sysOperatorService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    public SysUserController(SysUserService sysOperatorService) {
        this.sysOperatorService = sysOperatorService;
    }

    @ApiOperation(value = "分页查询用户列表")
    @GetMapping("/page")
    public ResultUtil<IPage<SysUserEntity>> page(SysSearchPage request, HttpServletRequest req) {
        request.setSysBranchOfficeId(this.getSysBranchOfficeId(req));
        return sysOperatorService.pageSearch(request);
    }

    @ApiOperation(value = "分页查询用户列表")
    @GetMapping("/listInit")
    public ResultUtil<IPage<SysUserEntity>> listInit(SysSearchPage request, HttpServletRequest req) {
        if(request.getSysBranchOfficeId()==null)
            return ResultUtil.success(ResultEnum.SUCCESS, "没有数据",null);
        return sysOperatorService.pageSearch(request);
    }

    @Log(title = "编辑操作员日志记录", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "添加操作员")
    @PostMapping("/save")
    public ResultUtil<SysUserEntity> save(@RequestBody SysUserEntity request,HttpServletRequest req) {
        request.setSysBranchOfficeId(this.getSysBranchOfficeId(req));
        return sysOperatorService.saveOperator(request);
    }

    @ApiOperation(value = "根据id获取操作员信息")
    @GetMapping("/getById/{id}")
    public ResultUtil<SysUserEntity> getById(@PathVariable Long id) {
        SysUserEntity operator = sysOperatorService.getById(id);
        return ResultUtil.success(operator);
    }

    @Log(title = "删除操作员日志记录", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/delete/{id}")
    public ResultUtil<SysUserEntity> delete(@PathVariable String id) {
        return sysOperatorService.deleteOperatorById(id);
    }

    @Log(title = "批量删除操作员日志记录", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除操作员")
    @DeleteMapping("/delete_bath")
    public ResultUtil<?> deleteBath(@RequestBody long[] ids) {
        return sysOperatorService.deleteBath(ids);
    }

    @ApiOperation(value = "禁用/启用操作员状态")
    @DeleteMapping("/update_enabled/{id}/{enabled}")
    public ResultUtil<?> updateEnabled(@PathVariable Long id, @PathVariable Boolean enabled) {
        return sysOperatorService.updateEnabled(id,enabled);
    }

    @ApiOperation(value = "获取所有操作员信息")
    @GetMapping("/findAll")
    public ResultUtil<List<SysUserEntity>> findAll(HttpServletRequest req) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("sys_branch_office_id",this.getSysBranchOfficeId(req));
        return ResultUtil.success(sysOperatorService.list(queryWrapper));
    }

    @Log(title = "更新个人信息日志记录", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "更新个人信息")
    @PostMapping("/updateMyInfo")
    public ResultUtil<SysUserEntity> updateMyInfo(@RequestBody SysUserEntity request) {
        sysOperatorService.updateById(request);
        return ResultUtil.success();
    }

    @Log(title = "重置个人密码日志记录", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "重置个人密码")
    @PostMapping("/pwd")
    public ResultUtil<SysUserEntity> pwd(@RequestBody SysUserPwdVo request) {
        SysUserEntity entity = new SysUserEntity();
        entity.setId(request.getUserid());
        entity.setPassword(passwordEncoder.encode(request.getPwd()));
        sysOperatorService.updateById(entity);
        return ResultUtil.success();
    }
}
